import axios from "axios"
import config from "@/config/env.js";


/* const serverUrl = globals.$config?.serverUrl || 'http://localhost:8080'; */
//创建一个axios对象出来
const request = axios.create({
    baseURL: config.apiUrl,
    //定义统一的请求头
    headers: {
        'Content-Type': "application/json;charset-UTF-8"
    },
    timeout: 5000
})
const userData = localStorage.getItem('user');
const token = userData ? JSON.parse(userData).token : '';
request.defaults.headers.common['Authorization'] = 'Bearer ' + token;

//request拦截器
//可以自请求发送前对请求做一些处理
//比如同一加token，对请求参数统一加密
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    // config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('user').token;
    //config.headers['token'] = user.token;//设置请求头
    return config
}, error => {
    return Promise.reject(error)
});

//response拦截器
//可以在接口响应后统一处理结果
request.interceptors.response.use(
    response => {
        //response.data即为后端返回的Result
        let res = response.data;
        //兼容服务端返回的字符串数据
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }
        return res;
    },
    error => {
        console.log('err' + error)// for debug
        return Promise.reject(error)
    },
)

export default request

